package com.bw.day03.xq

import androidx.lifecycle.viewModelScope
import com.bw.base.net.entity.AddCarEntity
import com.bw.base.net.entity.PreOrderEntity
import com.bw.base.net.entity.Res
import com.bw.base.net.entity.XqEntity
import com.bw.base.net.mvvm.BaseViewModel
import com.bw.base.net.param.ParamPreOrder
import com.bw.day03.utlis.SingleLiveData
import kotlinx.coroutines.launch

class XqViewModel: BaseViewModel() {
    val xqSuccess= SingleLiveData<Res<XqEntity>>()
    val xqFail= SingleLiveData<String>()
    fun xq(id: Int, type: String){
        viewModelScope.launch {
            try {
                xqSuccess.postValue(apiService.xq(id,type))
            }catch (e: Exception){
                xqFail.postValue( e.message.toString())
            }
        }
    }
    val addSuccess= SingleLiveData<Res<AddCarEntity>>()
    val addFail= SingleLiveData<String>()
    fun addCar(cartNum: Int,productAttrUnique: String,productId: Int){
        viewModelScope.launch {
            try {
                val map=mutableMapOf<String, Any>()
                map["cartNum"]=cartNum
                map["productAttrUnique"]=productAttrUnique
                map["productId"]=productId
                addSuccess.postValue(apiService.addCar(mapToBody(map)))

            }catch (e: Exception){
                addFail.postValue( e.message.toString())
            }
        }
    }
    val loveSuccess= SingleLiveData<Res<String?>>()
    val loveFail= SingleLiveData<String>()
    fun love(category: String,id: Int){
        viewModelScope.launch {
            try {
                val map=mutableMapOf<String, Any>()
                map["category"]=category
                map["id"]=id
                loveSuccess.postValue(apiService.love(mapToBody(map)))
            }catch (e: Exception){
                loveFail.postValue( e.message.toString())
            }
        }
    }

    val noLoveSuccess= SingleLiveData<Res<String?>>()
    val noLoveFail= SingleLiveData<String>()
    fun noLove(proId:Int){
        viewModelScope.launch {
            try {
                noLoveSuccess.postValue(apiService.noLove(proId))
            }catch (e: Exception){
                noLoveFail.postValue( e.message.toString())
            }
        }
    }

    val preSuccess= SingleLiveData<Res<PreOrderEntity>>()
    val preFail= SingleLiveData<String>()
    fun preOrder(paramPreOrder: ParamPreOrder){
        viewModelScope.launch {
            try {
                preSuccess.postValue(apiService.preOrder(paramPreOrder))
            }catch (e: Exception){
                preFail.postValue( e.message.toString())
            }
        }
    }
}